package cn.jishiyu11.xeqb.adapter

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Message
import android.support.v4.view.ViewPager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import cn.jishiyu11.xeqb.EntityClass.FragmengData.Mock_HomeBean
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.utils.*
import cn.jishiyu11.xeqb.webview.X5WebViewActivity
import java.util.ArrayList
import cn.jishiyu11.xeqb.adapter.HomeViewPagerAdaper

/**
 * Created by jsy_zj on 2018/1/15.
 */
class HomeRecyclerViewAdapter(context: Context, msgList: MutableList<Mock_HomeBean.Data>?, ISPAUSE: Boolean) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var context: Context? = null
    var list: MutableList<Mock_HomeBean.Data>? = null
    var inflater: LayoutInflater? = null
    var CHANGE_PAGER = 1
    var CHANTE_PAAGER_TIME: Long = 3000
    var ISPAUSE = false
    val TYPE_FOOT = 100


    init {
        this.context = context
        this.list = msgList
        this.inflater = LayoutInflater.from(this.context)
        this.ISPAUSE = ISPAUSE
    }

    var hanlder: Handler? = null


    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        var view: View? = null
        when (viewType) {
            0 -> {
                view = inflater?.inflate(R.layout.frg_home_listitem_viewpager, parent, false)
                return ViewHolder0(view, context!!)
            }
            1 -> {
                view = inflater?.inflate(R.layout.frg_home_listitem_muchitems1, parent, false)
                return ViewHolder1(view, context!!)
            }
            2 -> {
                view = inflater?.inflate(R.layout.frg_home_listitem_singleitem1, parent, false)
                return ViewHolder2(view, context!!)
            }
            3 -> {
                view = inflater?.inflate(R.layout.frg_home_listitem_singbigimg1, parent, false)
                return ViewHolder3(view, context!!)
            }
            4 -> {
                view = inflater?.inflate(R.layout.msg_recycler_item_text1, parent, false)
//                view = inflater?.inflate(R.layout.frg_home_listitem_singletexts1, parent, false)
                return ViewHolder4(view, context!!)
            }
            5 -> {
                view = inflater?.inflate(R.layout.frg_home_listitem_muchimgs1, parent, false)
                return ViewHolder5(view, context!!)
            }
            6 -> {
                view = inflater?.inflate(R.layout.frg_home_listitem_singleitem1_round, parent, false)
                return ViewHolder6(view, context!!)
            }
            7->{
                view = inflater?.inflate(R.layout.frg_home_listitem_singleitem1_bank, parent, false)
                return ViewHolder7(view, context!!)
            }
            TYPE_FOOT ->{
                view = inflater?.inflate(R.layout.footermore, parent, false)
                return ViewHolderLoadMore(view, context!!)
            }
        }

        return ViewHolder0(view, context!!)
    }

    override fun getItemCount(): Int {
        if (list != null && list?.size!! > 0) {
            return list?.size!! + 1
        } else {

            return 0
        }
//        return list?.size ?: 0
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        if (position >= list?.size!!) {

        } else {
            var bean = list?.get(position)
            if (holder is ViewHolder0) {
                //banner图

                var imgVList: MutableList<ImageView> = ArrayList()
                var indicatorList = ArrayList<ImageView>()
                var imgurls = bean?.image?.split(",")
                var links = bean?.links?.split(",")

                var count = imgurls?.size
                if (count == 1) {
                    bean?.image = bean?.image + "," + bean?.image
                    imgurls = bean?.image?.split(",")
                    count = imgurls?.size
                }

                if (count!! >= 2) {
                    for (i in 0 until (count * 3)) {
                        var imgV = ImageView(context)
                        imgurls?.get(i % count)?.let { ImageLoadUtils.display(context!!, imgV, it) }
                        imgV.setOnClickListener {
                            var intent = Intent(context, X5WebViewActivity::class.java)
                            intent.putExtra("url", links?.get(i % count!!))
                            context?.startActivity(intent)
                        }
                        imgVList?.add(imgV)
                    }
                } else {
                    for (i in 0 until count) {
                        var imgV = ImageView(context)
                        imgurls?.get(i)?.let { ImageLoadUtils.display(context!!, imgV, it) }
                        imgV.setOnClickListener {
                            var intent = Intent(context, X5WebViewActivity::class.java)
                            intent.putExtra("url", links?.get(i))
                            context?.startActivity(intent)
                        }
                        imgVList?.add(imgV)
                    }
                }

                if (indicatorList.size != count) {//设置indicator指示器
                    holder?.indicator.removeAllViews()
                }
                addIndicatorList(count, indicatorList, holder)

                holder.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                    override fun onPageScrollStateChanged(state: Int) {
                    }

                    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                    }

                    override fun onPageSelected(position: Int) {
                        if (position == imgVList.size - 2) {
                            holder.viewpager.currentItem = count!! * 2 - 2
                        }
                        if (position == 1) {
                            holder.viewpager.currentItem = count!! + 1
                        }
                        //设置指示器
                        indicatorShow(position % count!!, indicatorList)
                    }
                })
//            holder.viewpager.pageMargin = 20
//            holder.viewpager.offscreenPageLimit = 3
                holder.viewpager.adapter = HomeViewPagerAdaper(imgVList)
                holder.viewpager.currentItem = count
//            holder.viewpager.setPageTransformer(false,ViewpagerScaleTransforme())
                indicatorShow(0, indicatorList)
                if (hanlder == null) {
                    initHandler(holder?.viewpager)
                    hanlder?.sendEmptyMessage(CHANGE_PAGER)
                }

            } else if (holder is ViewHolder1) {
                //列表式 小图文布局  多个
//            var imgurls = bean?.image?.split(",")
                bean?.image?.let { ImageLoadUtils.display(context!!, holder?.img, it) }
                holder?.title.text = bean?.title
                holder?.content.text = bean?.intro
                setOnClickListenerMethod(holder?.layout!!, context!!, bean?.links.toString())
            } else if (holder is ViewHolder2) {

                //列表式 大图文布局  单个
//            bean?.image?.let { ImageLoadUtils.displayRound(context!!, holder?.img, it) }
                bean?.image?.let { ImageLoadUtils.display(context!!, holder?.img, it) }
                holder?.title.text = bean?.title
                var content = bean?.intro?.replace("##", "\n")
                holder?.content.text = content
                holder?.content.visibility = View.VISIBLE
                holder?.from.text = bean?.source
                if (bean?.showbut.equals("1")) {
                    holder?.bt.text = bean?.buttxt
                    holder?.bt.visibility = View.VISIBLE
                }
                setOnClickListenerMethod(holder?.layout!!, context!!, bean?.links.toString())
            } else if (holder is ViewHolder3) {
                //单个大图
                bean?.image?.let { ImageLoadUtils.display(context!!, holder?.img, it) }
                holder?.title.text = bean?.title
                holder?.from.text = bean?.source
                setOnClickListenerMethod(holder?.layout!!, context!!, bean?.links.toString())
            } else if (holder is ViewHolder4) {
                //纯文本
                holder?.title!!.text = bean?.title
                holder?.content!!.text = bean?.intro?.replace("##", "\n")
                holder?.time!!.text = bean?.source
                holder?.from!!.text = bean?.addtime
                setOnClickListenerMethod(holder?.layout!!, context!!, bean?.links!!)

            } else if (holder is ViewHolder5) {
                //按钮区
                var imgButtonList: MutableList<Map<String, String>>? = ArrayList()
                var gridViewAdapter: CommonAdapter<Map<String, String>>? = null
                var imgurls = bean?.image?.split(",")
                var titles = bean?.title?.split(",")
                var links = bean?.links?.split(",")
                var count = imgurls?.size
                for (i in 0 until count!!) {
                    var map = HashMap<String, String>()
                    map.put("img", imgurls?.get(i).toString())
                    map.put("title", titles?.get(i).toString())
                    map.put("link", links?.get(i)!!)
                    imgButtonList?.add(map)
                }
                gridViewAdapter = object : CommonAdapter<Map<String, String>>(context!!, imgButtonList!!, R.layout.frg_home_listitem_gridview_item) {
                    override fun convertlistener(holder: ViewHolder, t: Map<String, String>) {
                    }

                    override fun convert(holder: ViewHolder, t: Map<String, String>) {
                        var iv: ImageView = holder.getView(R.id.frg_home_listitem_gridview_item_iv)
                        t.get("img")?.let { ImageLoadUtils.display(context!!, iv, it) }
                        setOnClickListenerMethod(iv, context!!, t.get("link").toString())
                        holder?.getTextview(R.id.frg_home_listitem_gridview_item_tv).text = t.get("title")
                    }
                }

                if (imgurls!!.size > 4) {
                    var wide = count * DisplayUtils.pt2px(context!!, 90F)
                    var params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(wide, LinearLayout.LayoutParams.MATCH_PARENT)
                    holder?.gridView.layoutParams = params
                    holder?.gridView.stretchMode = GridView.NO_STRETCH
                    holder?.gridView.columnWidth = DisplayUtils.pt2px(context!!, 80F)
                    holder?.gridView.horizontalSpacing = DisplayUtils.pt2px(context!!, 10F)
                    holder?.gridView.numColumns = count
                    holder?.gridView.adapter = gridViewAdapter
                } else {
                }
                holder?.gridView.numColumns = count
                holder?.gridView.adapter = gridViewAdapter
            }else if (holder is ViewHolder6){
                //带圆角的item
                bean?.image?.let { ImageLoadUtils.displayRound(context!!, holder?.img, it) }
                holder?.title.text = bean?.title
                var content = bean?.intro?.replace("##", "\n")
                holder?.content.text = content
                holder?.content.visibility = View.VISIBLE
                holder?.from.text = bean?.source
                setOnClickListenerMethod(holder?.layout!!, context!!, bean?.links.toString())


            }else if (holder is ViewHolder7){
                //带圆角的bank 列表图
                bean?.image?.let { ImageLoadUtils.display(context!!, holder?.img1, it) }
                holder?.title1.text = bean?.title
                var content = bean?.intro?.replace("##", "\n")
                holder?.content1.text = content
//                holder?.content.visibility = View.VISIBLE
//                holder?.from1.text = bean?.source
//                if (bean?.showbut.equals("1")) {
//                    holder?.bt.text = bean?.buttxt
//                    holder?.bt.visibility = View.VISIBLE
//                }
                setOnClickListenerMethod(holder?.layout1!!, context!!, bean?.links.toString())


            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        var type = 0
        if (position >= list?.size!!) {
            type = TYPE_FOOT
        } else {

            var data = list?.get(position)
            when (data?.type) {
                "1" -> type = 0
                "5" -> type = 1
                "4" -> type = 2
                "2" -> type = 3
                "6" -> type = 4
                "3" -> type = 5
                "7" -> type = 6
                "8" -> type = 7
            }
        }
        return type
    }


    class ViewHolder0(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var viewpager: ViewPager = itemView?.findViewById(R.id.frg_home_listitem_viewpager_vp)!!
        var indicator: LinearLayout = itemView?.findViewById(R.id.frg_home_listitem_viewpager_indicator)!!
    }

    class ViewHolder1(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var layout: LinearLayout = itemView?.findViewById(R.id.frg_home_listitem_muchitems1_layout)!!
        var img: ImageView = itemView?.findViewById(R.id.frg_home_listitem_muchitems1_img)!!
        var title: TextView = itemView?.findViewById(R.id.frg_home_listitem_muchitems1_title)!!
        var content: TextView = itemView?.findViewById(R.id.frg_home_listitem_muchitems1_content)!!
    }

    class ViewHolder2(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var layout: LinearLayout = itemView?.findViewById(R.id.frg_home_listitem_singleitem1_layout)!!
        var img: ImageView = itemView?.findViewById(R.id.frg_home_listitem_singleitem1_img)!!
        var title: TextView = itemView?.findViewById(R.id.frg_home_listitem_singleitem1_title)!!
        var content: TextView = itemView?.findViewById(R.id.frg_home_listitem_singleitem1_content)!!
        var from: TextView = itemView?.findViewById(R.id.frg_home_listitem_singleitem1_from)!!
        var seekcount: TextView = itemView?.findViewById(R.id.frg_home_listitem_singleitem1_seekcount)!!
        var bt: TextView = itemView?.findViewById(R.id.frg_home_listitem_singleitem1_bt)!!
    }

    class ViewHolder3(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var layout: LinearLayout = itemView?.findViewById(R.id.frg_home_listitem_singbigimg1_layout)!!
        var img: ImageView = itemView?.findViewById(R.id.frg_home_listitem_singbigimg1_img)!!
        var title: TextView = itemView?.findViewById(R.id.frg_home_listitem_singbigimg1_title)!!
        var from: TextView = itemView?.findViewById(R.id.frg_home_listitem_singbigimg1_from)!!
    }

    class ViewHolder4(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var layout: LinearLayout = itemView?.findViewById(R.id.msg_recycler_item_text1_layout)!!
        var title: TextView = itemView?.findViewById(R.id.msg_recycler_item_text1_title)!!
        var content: TextView = itemView?.findViewById(R.id.msg_recycler_item_text1_content)!!
        var time: TextView = itemView?.findViewById(R.id.msg_recycler_item_text1_time)!!
        var from: TextView = itemView?.findViewById(R.id.msg_recycler_item_text1_from)!!
    }

    class ViewHolder5(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var gridView: GridView = itemView?.findViewById(R.id.frg_home_listitem_muchimgs1_gridview)!!
    }

    class ViewHolder6(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var layout: LinearLayout = itemView?.findViewById(R.id.frg_home_listitem_singleitem1_layout_round)!!
        var img: ImageView = itemView?.findViewById(R.id.img)!!
        var title: TextView = itemView?.findViewById(R.id.title)!!
        var content: TextView = itemView?.findViewById(R.id.content)!!
        var from:TextView = itemView?.findViewById(R.id.from)!!

    }

    class ViewHolder7(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var layout: LinearLayout = itemView?.findViewById(R.id.frg_list_bankitem_layout)!!
        var layout1: LinearLayout = itemView?.findViewById(R.id.bank_item_layout1)!!
        var layout2: LinearLayout = itemView?.findViewById(R.id.bank_item_layout2)!!
        var img1: ImageView = itemView?.findViewById(R.id.img1)!!
        var title1: TextView = itemView?.findViewById(R.id.title1)!!
        var content1: TextView = itemView?.findViewById(R.id.content1)!!
        var img2: ImageView = itemView?.findViewById(R.id.img2)!!
        var title2: TextView = itemView?.findViewById(R.id.title2)!!
        var content2: TextView = itemView?.findViewById(R.id.content2)!!
    }

    class ViewHolderLoadMore(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var gridView: LinearLayout = itemView?.findViewById(R.id.footer_layout)!!
    }


    /**
     * 设置指示器
     */
    private fun indicatorShow(position: Int, indicatorList: MutableList<ImageView>) {
        if (indicatorList!!.size != 0) {
            for (i in indicatorList!!) {
                i.setImageResource(R.mipmap.dot_grey)
            }
            indicatorList!!.get(position).setImageResource(R.mipmap.dot_white)
        }
    }

    private fun addIndicatorList(count: Int, indicatorList: MutableList<ImageView>, holder: ViewHolder0) {
        for (i in 0 until count!!) {//设置indicator指示器
            var imgV = ImageView(context)
            var wid = DisplayUtils.dip2px(context!!, 10F)
            var margin = DisplayUtils.dip2px(context!!, 10F)
            var linearlayoutParams = LinearLayout.LayoutParams(wid, margin)
            linearlayoutParams.setMargins(margin, 0, 0, 0)
            imgV.layoutParams = linearlayoutParams
            imgV.setImageResource(R.mipmap.dot_grey)
            imgV.setScaleType(ImageView.ScaleType.MATRIX)
            indicatorList!!.add(imgV)
            holder.indicator.addView(imgV)
        }
    }

    /**
     * 初始handler
     * */
    private fun initHandler(vp: ViewPager?) {
        hanlder = object : Handler() {
            override fun handleMessage(msg: Message?) {
                var tag = msg?.what
                if (tag == CHANGE_PAGER) {
                    if (!ISPAUSE) {
                        vp?.currentItem = vp?.currentItem?.plus(1)!!
//                        activity.showShortToast("这是handler 的  消息")
                        hanlder?.sendEmptyMessageDelayed(CHANGE_PAGER, CHANTE_PAAGER_TIME)
                    }
                }
            }
        }
    }

    fun getAdapterHandler(): Handler? {
        return hanlder
    }


    fun setOnClickListenerMethod(view: View, context: Context, url: String) {
        view.setOnClickListener {
            if (!CheckImportantKeys(context, url)) {
                var intent = Intent(context, X5WebViewActivity::class.java)
                intent.putExtra("url", url)
                context?.startActivity(intent)
            }
        }
    }

}